<?php
require_once ("../../includes/config.php");
if (isset($_GET['id']))
{
	$contracts = query('
          SELECT * FROM contracts 
          LEFT JOIN clients ON 
            contracts.client_id=clients.id 
          LEFT JOIN companies ON 
            contracts.company_id=companies.company_id 
          WHERE contracts.contract_id = ?', $_GET['id']);

	$contract = $contracts[0];
	$apartments = query("SELECT * FROM contract_apartments WHERE contract_id = ?", $contract['contract_id']);

	$contract['apart_name'] = "";
	$contract['apartment_id'] = "";

	$n = count($apartments);
	$i = 0;
	foreach ($apartments as $apartment)
	{
		$i++;

		$apartment = query("SELECT id,apart_name FROM apartments where id = ?", $apartment['apartment_id']);
		var_dump($contract['apart_name']);
		$contract['apart_name'] .= $apartment[0]['apart_name'];
		//take care of commas
		$contract['apartment_id'] .= $apartment[0]['id'];
		//take care of commas
		if ($i != $n)
		{
			$contract['apart_name'] .= ',';
			$contract['apartment_id'] .= '|';
		}
	}

	renderWithoutHeader("Bookings/edit.php", array("contract" => $contract));

}
?>